package cn.zhuama.basic.context;

import cn.zhuama.basic.util.JwtUtils;
import cn.zhuama.basic.util.LoginData;
import cn.zhuama.basic.util.Payload;
import cn.zhuama.basic.util.RsaUtils;
import cn.zhuama.user.domain.Logininfo;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContextUtil {
    public static Logininfo getLoginInfoFromReq(HttpServletRequest request)  {
        String jwtToken = request.getHeader("Token");
        Logininfo logininfo = null;
        PublicKey publicKey = null;
        try {
            publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
            Payload<LoginData> infoFromToken = JwtUtils.getInfoFromToken(jwtToken, publicKey, LoginData.class);
            logininfo = infoFromToken.getLoginData().getLogininfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return logininfo;

    }
}
